package edu.um.variable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RecordVariable extends Variable {

  Map<String, Variable> fields = new HashMap<String, Variable>();

  public RecordVariable(String name, List<Variable> theVariables) {
    super(name, VariableType.REGISTRO);
    if (theVariables == null || theVariables.size() == 0) {
      throw new IllegalArgumentException(
          "Record variables must contain at least an one inner variable");
    }
    for (Variable variable : theVariables) {
      VariableType type = variable.getType();
      if (type == VariableType.ARRAY || type == VariableType.REGISTRO) {
        throw new IllegalArgumentException(
            "No es posible crear un array o registro dentro de un registro");
      }
      if (fields.containsKey(name)) {
        throw new IllegalArgumentException("The variable " + name
            + "is already defined");
      }
      String fieldName = variable.getName();
      variable.setName(this.getName() + "." + fieldName);
      fields.put(fieldName, variable);
    }
  }

  /**
   *
   * @param fieldName
   * @return
   */
  public boolean hasField(String fieldName) {
    return fields.containsKey(fieldName);
  }

  public Variable getField(String fieldName)  {
    return fields.get(fieldName);
  }
}
